#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Init()
{
    this->setFixedSize(m_width, m_height);
    m_pLabel = new QLabel(this);
    m_pLabel->setGeometry(0, 0, m_width, m_height);
    m_pLabelGif = new QLabel(this);
    m_pLabelGif->setGeometry(0, 0, m_width, m_height);

    m_pMovie = NULL;
    m_changePicFlag = 0;

    if (GetLocalPicList()) {
        QString url = m_picPath + m_picList[m_changePicFlag];
        m_pLabel->setStyleSheet(QString("border-image:url(%1);").arg(url));
    } else {
        LabelSetTextNotFound();
    }

    m_pTimer = new QTimer(this);
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
    m_pTimer->start(m_mstc);
}

void MainWindow::ShowPng(int x, int y, int w, int h, const QString path)
{
    QLabel *label = new QLabel(this);
    label->setGeometry(x, y, w, h);
    label->setStyleSheet(QString("border-image:url(%1);").arg(path));
    label->show();
}

void MainWindow::ShowGif(int x, int y, int w, int h, const QString path)
{
    QLabel *labelGif = new QLabel(this);
    labelGif->setGeometry(x, y, w, h);
    QMovie *movie = new QMovie(path, QByteArray(), this);
    movie->setScaledSize(labelGif->size());
    movie->start();
    labelGif->setMovie(movie);
    labelGif->show();
}

void MainWindow::onTimeHqv()
{
    m_index = (m_index+1)%m_imageList.size();
    QString path = m_imageList.at(m_index);
    m_pHqvLabel->setStyleSheet(QString("border-image:url(%1);").arg(path));
}

void MainWindow::ShowHqv(int x, int y, int w, int h, const QString path)
{
    for (int i = 1; i <= 18; i++)
        m_imageList.push_back(path + QString("%1.png").arg(i));

    m_index = 0;
    m_pHqvLabel = new QLabel(this);
    m_pHqvLabel->setGeometry(x, y, w, h);

    m_pTimerHqv = new QTimer(this);
    connect(m_pTimerHqv, SIGNAL(timeout()), this, SLOT(onTimeHqv()));
    m_pTimerHqv->start(100);
}

void MainWindow::ShowOnePage()
{
    int width = 1920;
    int height = 290;
    this->setFixedSize(width, height);

    ShowPng(0, 0, width, height, "./shenyang10/one_stationBG.png");
    ShowGif(1354, 25, 385, 167, "./shenyang10/door_leave_open.gif");
    ShowGif(591, 16, 194, 71, "./shenyang10/four_name_cur_7.gif");
    ShowHqv(665, 91, 48, 50, "./shenyang10/hqv/");
}

void MainWindow::SetBaseData(int mstc, int width, int height, const QString &picPath)
{
    if ("/" != picPath.right(1))
        m_picPath = picPath + "/";
    else
        m_picPath = picPath;
    m_width = width;
    m_height = height;
    m_mstc = mstc;

    Init();
}

void MainWindow::LabelSetTextNotFound()
{
    m_changePicFlag = 0;
    m_pLabel->clear();
    m_pLabel->setText("please add png or gif files to " + m_picPath);
    m_pLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_pLabel->show();
}

bool MainWindow::GetLocalPicList()
{
    QDir dir(m_picPath);
    if (dir.exists()) {
        QStringList nameFilters;
        nameFilters << "*.png" << "*.gif";
        QStringList picList = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
        if (picList.size() == 0) {
            return false;
        } else {
            if (m_picList != picList) {
                m_picList.clear();
                m_changePicFlag = 0;
                m_picList = picList;
            }
        }
    } else return false;

    return true;
}

void MainWindow::onTimeout()
{
    if (!GetLocalPicList()) {
        LabelSetTextNotFound();
        return;
    }

    m_pLabel->clear();

    QString url = m_picPath + m_picList[m_changePicFlag];

    if ("png" == url.right(3))
    {
        m_pLabelGif->hide();
        m_pLabel->setStyleSheet(QString("border-image:url(%1);").arg(url));
        m_pLabel->show();

    } else if ("gif" == url.right(3)) {

        if (m_pMovie == NULL)
            m_pMovie = new QMovie(url, QByteArray(), this);
        else
            m_pMovie->stop();
        m_pMovie->setFileName(url);
        m_pMovie->setScaledSize(m_pLabelGif->size());
        m_pMovie->start();
        m_pLabelGif->setMovie(m_pMovie);
        m_pLabelGif->show();

    } else {}

    if (++m_changePicFlag == m_picList.size()) m_changePicFlag = 0;
}



